<?php

/**图片上传 组件
*使用示例:<?php $this->widget('ext.datepickerwidget',array('model'=>$model,'name'=>'vip_deadline','style'=>'border:1px solid #c5c5c5'));?>
*/

class UploadpicWidget extends CWidget
{
	public $model=null;
	public $name='date';//选择框变量名
	public function init()
	{

	}

	public function run()
	{
		$img = '';
		$model = $this->model;
		$name = $this->name;
		$pics = $model->$name;
		
		echo '<link rel="stylesheet" href="'.Yii::app()->request->baseUrl.'/public/css/uploadpic/uploadpic.css" type="text/css">';

		echo '<input style="display:none;" name="'.get_class($this->model).'['.$this->name.']" id="hiddenpicipt" type="text" value="">';
		echo '<div class="main">
				<div id="showimg">
					<ul id="showui">
					</ul>

					<div id="showinput">

					</div>
				</div>
				<div class="upimg">
					<input type="file" id="upgteimg" value="" multiple />
				</div>
				<div class="btn" id="submit" style="width: 48px;">开始上传</div>
			</div>';

		echo '<script src="'.Yii::app()->request->baseUrl.'/public/js/uploadpic/uploadpic.js?cccc"></script>';
		if($pics){
			$arr = array_filter(explode(',', $pics)); 
			foreach ($arr as $key => $value) {
				$img .= '{name: "'.random(4,'abcdefghijklmnopqrstuvwxyz').'", base64: "'.base64EncodeImage('../img/'.$value).'"},';
				$imgArr[] = array(
					'name' => random(4,'abcdefghijklmnopqrstuvwxyz'),
					'base64' => base64EncodeImage('../img/'.$value),
				);
			}
			$img = rtrim($img,',');
			echo '<script>var dataArr = '.json_encode($imgArr).';editimg();</script>';
		}else{
			echo '<script>var dataArr = [];</script>';
		}
		

		echo '<script>
			function send() {
				var str = "";
				for(var j = 0; j < dataArr.length; j++) {
					str += dataArr[j].base64+"[explode]";
				}
				$("#hiddenpicipt").val(str);
				alert("添加成功");
			}</script>';
	}

	protected  function registerClientScript()
	{
		$cs=Yii::app()->clientScript;
	    $cs->registerScriptFile(Yii::app()->request->baseUrl."/public/js/uploadlogo/cropper.min.js");	
	}
}